package com.beidouapp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.beidouapp.common.util.PasswordAuthenticatorUtil;
import com.beidouapp.dao.UserDao;
import com.beidouapp.domain.pojo.User;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.service.UserService;

/**
 * Created by mhuang on 2016/9/7.
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public boolean checkPassword(String loginName, String password) throws BusinessException {
    	 User suser =  userDao.getUserWithPSWByLoginName(loginName);
         if(suser==null){
         	return false;
         }
         boolean  s_flag = PasswordAuthenticatorUtil.comparePassword(password, suser.getPassword());
         return s_flag;
    }

    @Override
    public User getUserWithNameAndPassword(String loginName, String password) throws BusinessException {
        User suser =  userDao.getUserWithPSWByLoginName(loginName);
        if(suser==null){
        	return null;
        }
        boolean  s_flag = PasswordAuthenticatorUtil.comparePassword(password, suser.getPassword());
        if( s_flag){
        	return suser;
        }
        return null;
       
    }

    @Override
    public void resetPassword(Long userId, String password) {
        User user = userDao.getEntryByID(userId);
        if (user == null) {
            throw new BusinessException("用户不存在.");
        }
        user.setPassword(password);
        this.resetPassword(user);
    }

    private void resetPassword(User user) {
        logger.info("call UserManagerImpl.modifyUser, user:" + user);
        if (StringUtils.isBlank(user.getLoginName())) {
            logger.error("UserManagerImpl.resetPassword Error:PMS-ERR-01010201,用户名不能为空");
            throw new BusinessException("PMS-ERR-01010201", "用户名不能为空");
        }
        logger.info("call BaseDao.modify, Object:" + user);
        if (null != user.getPassword()) {
            String newPassword = user.getPassword();
            try {
                newPassword = PasswordAuthenticatorUtil.encryptPassword(newPassword);
                user.setPassword(newPassword);
            } catch (Exception e) {
                logger.error("Error:PMS-ERR-01010400,重置密码错误。");
                throw new BusinessException("PMS-ERR-01010400", "重置密码错误。");
            }
        }
        userDao.resetPassword(user);
    }

    @Override
    public void changeUserPassword(Long userId, String loginName, String oldPassword, String newPassword) throws BusinessException {

        try {
            newPassword=PasswordAuthenticatorUtil.encryptPassword (newPassword);
        } catch (Exception e) {
            logger.error ("Error:PMS-ERR-01010400,创建密文错误。");
            throw new BusinessException ("PMS-ERR-01010400", "修改密码错误。");
        }
        if(userId == null){
            logger.error ("Error:PMS-ERR-01010401,用户ID不能为空");
            throw new BusinessException ("PMS-ERR-01010401", "用户ID不能为空");
        }
        if (StringUtils.isBlank (loginName))
        {
            logger.error ("Error:PMS-ERR-01010401,用户名为空");
            throw new BusinessException ("PMS-ERR-01010401", "用户名为空");
        }
        if (StringUtils.isBlank (oldPassword))
        {
            logger.error ("Error:PMS-ERR-01010402,原密码为空");
            throw new BusinessException ("PMS-ERR-01010402", "原密码为空");
        }
        if (StringUtils.isBlank (newPassword))
        {
            logger.error ("Error:PMS-ERR-01010403,新密码为空");
            throw new BusinessException ("PMS-ERR-01010403", "新密码为空");
        }
        if (!checkPassword (loginName, oldPassword))
        {
            logger.error ("Error:PMS-ERR-01010404,用户名【" + loginName + "】不存在或原密码不正确");
            throw new BusinessException ("PMS-ERR-01010404", "用户名不存在或原密码不正确");
        }

        Map<String, String> map = new HashMap<String, String>();
        map.put ("loginname", loginName);
        map.put ("new_password", newPassword);
        logger.info ("call BaseDao.modify, SQL-map:User.changePSW, map:" + map);

        User user = new User();
        user.setId(userId);
        user.setPassword(newPassword);
        userDao.resetPassword(user);
    }

    @Override
    public User getUser(String loginName) {
        return userDao.getUserByLoginName(loginName);
    }

    @Override
    public User addUser(User user) throws BusinessException {
        return null;
    }

    @Override
    public User removeUser(String loginname) throws BusinessException {
        return null;
    }

    @Override
    public List<User> getAll() {
        return null;
    }

    @Override
    public void changePassword(String loginname, String oldPassword, String newPassword) throws BusinessException {

    }


    @Override
    public List<User> findUserByOrganizationCode(String organizationCode) {
        return null;
    }
    
    public static void main(String[] args) {
        String enc_password = PasswordAuthenticatorUtil.encryptPassword("1234");
        System.out.println(enc_password);
	}

	@Override
	public User getUserWithName(String loginName) throws BusinessException {
		return userDao.getUserWithPSWByLoginName(loginName);
	}
}
